package org.estela.util;

public class Math2 {
	
	/**
	 * Smaller possible constant -> small deviation allowed in double
	 * comparisons
	 */
	public static final double SMALL_CONSTANT = 1e-6;
	
	/** log_2 */
	public static final double log2 = Math.log(2);

	/**
	 * Returns the logarithm of a for base 2.
	 * 
	 * @param num -
	 *            a number
	 */
	public static double log_2(double num) {
		return Math.log(num) / log2;
	}	

	/**
	 * Normalizes the values in the array using the sum.
	 * 
	 * @param doubles - array of doubles
	 * @param sum -
	 *            value used to normilize the array of doubles
	 * @exception IllegalArgumentException -
	 *                if sum is zero, NaN or infinity if sum is zero or NaN
	 */
	public static void normalize(double[] doubles, double sum) {

		if (Double.isNaN(sum) || Double.isInfinite(sum) || sum == 0) {
			throw new IllegalArgumentException("Invalid sum: "
					+ Double.toString(sum));
		}
		for (int i = 0; i < doubles.length; i++) {
			doubles[i] /= sum;
		}
	}
	
	/**
	 * 
	 * @param doubles
	 */
	public static void normalize(double[] doubles) {
	    double sum = 0;
	    for (int i = 0; i < doubles.length; i++) {
	      sum += doubles[i];
	    }
	    normalize(doubles, sum);
	}	

}
